using Ke.Bee.Shared.Models.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace Ke.Bee.Shared.Abstractions;

public static class IHubContextExtensions
{
    /// <summary>
    /// 发送进度更新到指定连接客户端
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="hubContext"></param>
    /// <param name="connectionId"></param>
    /// <param name="arg"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public static Task SendProgressUpdateAsync<T>(this IHubContext<T> hubContext,
        TaskProgress taskProgress,
        CancellationToken cancellationToken = default)
        where T : Hub
    {
        return hubContext.SendProgressUpdateAsync(taskProgress.ConnectionId,
            taskProgress.TaskId,
            taskProgress.Progress,
            taskProgress.Status,
            cancellationToken)
            ;
    }

    /// <summary>
    /// 发送进度更新到指定连接客户端
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="hubContext"></param>
    /// <param name="connectionId"></param>
    /// <param name="taskId"></param>
    /// <param name="progress"></param>
    /// <param name="status"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public static Task SendProgressUpdateAsync<T>(this IHubContext<T> hubContext,
        string connectionId,
        string taskId,
        int progress,
        string status,
        CancellationToken cancellationToken = default)
        where T : Hub
    {
        return hubContext.Clients.Client(connectionId)
            .SendAsync("ProgressUpdate", new
            {
                TaskId = taskId,
                Progress = progress,
                Status = status,
                // Timestamp = DateTimeOffset.Now
            }, cancellationToken)
            ;
    }
}